var CONST_USER_ID = "userId";
var CONST_BUSI_TYPE = "BUSI_TYPE";
var CONST_SHUI_FEI = "S";
var CONST_DIAN_FEI = "D";
var CONST_QI_FEI = "Q";
var CONST_TV_FEI = "T";
var CONST_USER_QUERY_ = "userQuery_";
var CONST_USER_QUERY_UTIL = "userQueryUtil";
var CONST_USER_QUERY_RESULT = "userQueryResult";
var CONST_CONS_NO = "consNo";
var CONST_TV_PACKAGE = "tvPackage";
var CONST_TV_MONTH = "tvMonth";
var CONST_PAY_MONTY = "payMoney";
var CONST_DEBT_MONEY = "debtMoney";

sessionStorage.setItem(CONST_DEBT_MONEY, 0);
sessionStorage.setItem(CONST_TV_MONTH, 0);

$(document).ready(function() {
    <!--初始化主页-->
    $("#aShui").bind("click", function() {
        sessionStorage.setItem(CONST_BUSI_TYPE, CONST_SHUI_FEI);
        $.mobile.changePage("#pagePayment", {transition: "slide"});
    });
    $("#aDian").bind("click", function() {
        sessionStorage.setItem(CONST_BUSI_TYPE, CONST_DIAN_FEI);
        $.mobile.changePage("#pagePayment", {transition: "slide"});
    });
    $("#aQi").bind("click", function() {
        sessionStorage.setItem(CONST_BUSI_TYPE, CONST_QI_FEI);
        $.mobile.changePage("#pagePayment", {transition: "slide"});
    });
    $("#aTV").bind("click", function() {
        sessionStorage.setItem(CONST_BUSI_TYPE, CONST_TV_FEI);
        $.mobile.changePage("#pagePayment", {transition: "slide"});
    });

    <!--初始化缴费页-->
    $("#aAgreement").bind("click", function(event){
        $.mobile.changePage("#pageAgreement", {transition : "slide"});
    });
});

<!-- pagePayment页面中电视费特定字段的样式值 -->
var labelComboStyle = "height: 40px; margin-top: 20px;";
var labelInputStyle = "height: 38px; margin-top: 15px;";
<!--跳转到缴费页面时的参数及页面处理-->
$(document).delegate("#pageHome", "pageinit", function() {
    $(document).bind("pagebeforechange", beforeChangePage);
});
$(document).delegate("#pagePayment", "pageinit", function() {
    $(document).bind("pagebeforechange", beforeChangePage);
});
$(document).delegate("#pagePay", "pageinit", function() {
    $(document).bind("pagebeforechange", beforeChangePage);
});
$(document).delegate("#pagePayResults", "pageinit", function() {
    $(document).bind("pagebeforechange", beforeChangePage);
});
$(document).delegate("#pagePayConfirm", "pageinit", function() {
    $(document).bind("pagebeforechange", beforeChangePage);
});
$(document).delegate("#pageQueryResult", "pageinit", function() {
    $(document).bind("pagebeforechange", beforeChangePage);
});

function beforeChangePage(e, data) {
    if(data.options.target) {
        var strs = data.options.target.split(/[#,?,&]/);
        if (strs.indexOf("pageHome") >= 0) {
            if(strs[2]) {
                if(strs[2].split("=")[0] == 'user') {
                    sessionStorage.setItem(CONST_USER_ID, strs[2].split("=")[1]);
                    window.location = "/wxcs/index.html?20150128";
                }
            }
        } else if (strs.indexOf("pagePayResults") >= 0) {
            for(var j=0; j<strs.length; j++) {
                if (strs[j].split("=")[0] == 'payRs') {
                    if (strs[j].split("=")[1] == 1) {
                        $('#paySuccess').show();
                        $('#payFailure').hide();
                    } else {
                        $('#paySuccess').hide();
                        $('#payFailure').show();
                    }
                    break;
                }
            }
        } else if (strs.indexOf("pagePayment") >= 0) {
            var pagePayment = $("#pagePayment");
            var type = sessionStorage.getItem(CONST_BUSI_TYPE);
            if (type == CONST_SHUI_FEI) {
                pagePayment.find("#title").html("水费");
                hideTVField();
            } else if (type == CONST_DIAN_FEI) {
                pagePayment.find("#title").html("电费");
                hideTVField();
            } else if (type == CONST_QI_FEI) {
                pagePayment.find("#title").html("燃气费");
                hideTVField();
            } else if (type == CONST_TV_FEI) {
                pagePayment.find("#title").html("电视费");
                showTVField();
            }

            common.setAreaCombo($("#city"), type);
            $("#company").empty();
            common.setUtilCombo($("#company"), 0, type);
            $("#city").change(function () {
                $("#company").empty();
                common.setUtilCombo($("#company"), $(this).val(), type);
            });
            document.getElementById("userNumber").value = "";
            document.getElementById("payMonth").value = "";

        } else if (strs.indexOf("pagePay") >= 0) {
            if(sessionStorage.getItem(CONST_BUSI_TYPE) == CONST_TV_FEI) {
                $('#prepayMonthText').show();
                $('#prepayMonth').show();
                $('#prepayMonth').html(sessionStorage.getItem(CONST_TV_MONTH) + "个月");
                $('#prepayBalText').hide();
                $('#prepayBal').hide();
                $('#moneyText').hide();
                $('#moneyValue').hide();
                $('#tip').hide();

                $('#debtText').html("缴费金额");
            } else {
                $('#prepayMonthText').hide();
                $('#prepayMonth').hide();
                $('#prepayBalText').show();
                $('#prepayBal').show();
                $('#moneyText').show();
                $('#moneyValue').show();
                $('#tip').show();

                $('#debtText').html("欠费金额");
            }
            $('#consNo').html(sessionStorage.getItem(CONST_CONS_NO));
            var data = sessionStorage.getItem(CONST_USER_QUERY_ + sessionStorage.getItem(CONST_CONS_NO));
            if (data) {
                data = eval("(" + data + ")");
                $('#userName').html(data.USER_NAME);
                $('#contNo').html(data.CONT_NO);
                $('#prepayBal').html(data.PREPAY_BAL);
                $('#debtMoney').html(data.DEBT_MONEY);
                sessionStorage.setItem(CONST_DEBT_MONEY, data.DEBT_MONEY);
            }
        } else if (strs.indexOf("pagePayConfirm") >= 0) {
            $('#consNoQ').html(sessionStorage.getItem(CONST_CONS_NO));
            $('#payMoneyQ').html(sessionStorage.getItem(CONST_PAY_MONTY));
            var data = sessionStorage.getItem(CONST_USER_QUERY_ + sessionStorage.getItem(CONST_CONS_NO));
            if (data) {
                data = eval("(" + data + ")");
                $('#userNameQ').html(data.USER_NAME);
                $('#contNoQ').html(data.CONT_NO);
                $('#prepayBalQ').html(data.PREPAY_BAL);
            }
        } else if (strs.indexOf("pageQueryResult") >= 0) {
            var data = sessionStorage.getItem(CONST_USER_QUERY_RESULT);
            if(data) {
                data = eval("(" + data + ")");
                var html = '';
                if(data.ORDER_LIST) {
                    for(var i=0; i< data.ORDER_LIST.LENGTH; i++) {
                        html += '<li style="padding-bottom: 15px;"><div class="ui-grid-a">';
                        html += '<div class="ui-block-a" style="padding-top: 5px;">交易日期</div>';
                        html += '<div class="ui-block-b" style="padding-top: 5px; color: #ff9c13;">' + data.ORDER_LIST.ITEM[i].DATE + '</div>';

                        html += '<div class="ui-block-a" style="padding-top: 5px;">交易时间</div>';
                        html += '<div class="ui-block-b" style="padding-top: 5px; color: #ff9c13;">' + data.ORDER_LIST.ITEM[i].TIME + '</div>';

                        html += '<div class="ui-block-a" style="padding-top: 5px;">收费单位</div>';
                        html += '<div class="ui-block-b" style="padding-top: 5px; color: #ff9c13;">' + data.ORDER_LIST.ITEM[i].UTIL_ID + '</div>';

                        html += '<div class="ui-block-a" style="padding-top: 5px;">缴&nbsp;费&nbsp;号</div>';
                        html += '<div class="ui-block-b" style="padding-top: 5px; color: #ff9c13;">' + data.ORDER_LIST.ITEM[i].CONS_NO + '</div>';

                        html += '<div class="ui-block-a" style="padding-top: 5px;">付款状态</div>';
                        html += '<div class="ui-block-b" style="padding-top: 5px; color: #ff9c13;">' + common.statusCode[data.ORDER_LIST.ITEM[i].STATUS] + '</div>';

                        html += '<div class="ui-block-a" style="padding-top: 5px;">交易金额</div>';
                        html += '<div class="ui-block-b" style="padding-top: 5px; color: #e40177;">' + data.ORDER_LIST.ITEM[i].AMT + '</div>';
                        html += '</div></li>';
                    }
                } else {
                    html = '<li>' + data.NOTE + '</li>';
                }
                $('#ulResult').html(html);
                $('#ulResult').listview('refresh');
            }
        }
    }
}

function hideTVField() {
    document.getElementById("tvPackageLabel").setAttribute("style", "display:none");
    document.getElementById("tvPackageField").setAttribute("style", "display:none");
    document.getElementById("payMonthLabel").setAttribute("style", "display:none");
    document.getElementById("payMonthField").setAttribute("style", "display:none");
}

function showTVField() {
    document.getElementById("tvPackageLabel").setAttribute("style", labelComboStyle);
    document.getElementById("tvPackageField").setAttribute("style", "");
    document.getElementById("payMonthLabel").setAttribute("style", labelInputStyle);
    document.getElementById("payMonthField").setAttribute("style", "");
}